/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 *
 * @author: jin Chen 
 * @date: Dec 6, 2005$
 * @version: 1.0
 */
package edu.psu.geovista.app.gmatrix.data;

import edu.psu.geovista.app.gmatrix.canvas.Cell;
import edu.psu.geovista.app.gmatrix.canvas.DfCell;
import edu.psu.geovista.app.gmatrix.canvas.RectangleSymbol;
import edu.psu.geovista.app.gmatrix.canvas.Symbol;

import java.awt.*;
import java.util.Vector;

public class DfMatrixModel extends JRowHeadDfTableModel  implements BasicMatrixmodel {
    Color defaultForeground=Color.gray;
    Color defaultBackground=Color.black;
    Symbol defaultSymbol=new RectangleSymbol();


      /**
     * add a row
     * @param colors  color for the row
     */
    /*public void addRow(Color[] colors){
        int nbcol = colors.length;
        int colCount = this.getColumnCount() ;
        if(colCount >0&&nbcol !=colCount){
           new IllegalArgumentException(this.getClass().getName() +" is passed a color with unexpected length.");
        }
          Cell[] cells = createCells( colors);
          this.addRow(cells);

    }*/



    /**
     * add a column
     * @param colors  color for the column
     */
   /*public void addColumn(Color[] colors){
        int nbrow = colors.length;
        int rowCount = this.getRowCount() ;
        if(rowCount >0&&nbrow !=rowCount){
           new IllegalArgumentException(this.getClass().getName() +" is passed a color with unexpected length.");
        }
        Cell[] cells = createCells(colors);
        this.addColumn("no name",cells);
    }*/



    /*******************************************************************************************************
     *                default values
     *******************************************************************************************************/
    public Color getDefaultBackground() {
        return defaultBackground;
    }

    public void setDefaultBackground(Color defaultBackground) {
        this.defaultBackground = defaultBackground;
    }

    public Color getDefaultForeground() {
        return defaultForeground;
    }



    public void setDefaultForeground(Color defaultForeground) {
        this.defaultForeground = defaultForeground;
    }

    public Symbol getDefaultSymbol() {
        return defaultSymbol;
    }

    public void setDefaultSymbol(Symbol defaultSymbol) {
        this.defaultSymbol = defaultSymbol;
    }
    /*******************************************************************************************************
     *                setter/getter
     *******************************************************************************************************/


    

    public void setColors(Color[] colors) {
        Color[][] mycolors=new Color[1][colors.length ];
        mycolors[0]=colors;
        setColors(mycolors);
    }

    public void setColors(Color[][] colors) {
        int nbrow = colors.length;
        int nbcol = colors[0].length;
       /* Cell[][] cells=new Cell[nbrow][nbcol];
        for (int i=0;i<nbrow ;i++){
            //Cell[] arow = cells[i];
            for (int j=0;j<nbcol ;j++){
                DfCell cell = new DfCell();
                cell.setForeground(colors[i][j]);
                cells[i][j]  =cell;

            }
        }*/
         if((this.getRowCount() >0||this.getColumnCount() >0)&&
            (nbrow==getRowCount() ||nbcol==getColumnCount() ) ){
                     //data exists,
                //new IllegalArgumentException(this.getClass().getName() +" is passed a colors with unexpected length");

            //data length match cells
                for (int i=0;i<nbrow ;i++){
                    //Cell[] arow = cells[i];
                    for (int j=0;j<nbcol ;j++){
                        Cell cell = this.getCell(i,j);
                        cell.setForeground(colors[i][j]);
                    }
                }

        }
        else{//not data exists OR data exists but length different, reset it
            Cell[][] cells=new Cell[nbrow][nbcol];
            for (int i=0;i<nbrow ;i++){
                //Cell[] arow = cells[i];
                for (int j=0;j<nbcol ;j++){
                    Cell cell = new DfCell();
                    cell.setDatamodel(this);
                    cell.setForeground(colors[i][j]);
                    cells[i][j]  =cell;
                }
            }
            this.setDataVector(cells,new Object[nbcol]);
             updateRowColumIdentifier(nbrow,nbcol);
       }
        //this.setDataVector(cells,new Object[nbcol]);
        //this.dataVector.clear();


    }
      public void setBackgrounds(Color[][] backgrounds) {
        int nbrow = backgrounds.length;
        int nbcol = backgrounds[0].length;

       /* if(this.getRowCount() >0||this.getColumnCount() >0){
            //data exists
            if(nbrow!=getRowCount() ||nbcol!=getColumnCount() ) {
                new IllegalArgumentException(this.getClass().getName() +" is passed a backgrounds with unexpected length");
            }*/
          if((this.getRowCount() >0||this.getColumnCount() >0)&&
            (nbrow==getRowCount() ||nbcol==getColumnCount() ) ){
             //data length match cells
                for (int i=0;i<nbrow ;i++){
                    //Cell[] arow = cells[i];
                    for (int j=0;j<nbcol ;j++){
                        Cell cell = this.getCell(i,j);
                        cell.setBackground(backgrounds[i][j]);
                    }
                }

        }
        else{//not data exists
            Cell[][] cells=new Cell[nbrow][nbcol];
            for (int i=0;i<nbrow ;i++){
                //Cell[] arow = cells[i];
                for (int j=0;j<nbcol ;j++){
                    Cell cell = new DfCell();
                    cell.setDatamodel(this);
                    cell.setBackground(backgrounds[i][j]);
                    cells[i][j]  =cell;
                }
            }
            this.setDataVector(cells,new Object[nbcol]);
             updateRowColumIdentifier(nbrow,nbcol);
       }
    }

    /**
     * assume data exists
     * @param symbols
     */
    public void setSymbols(Symbol[][] symbols) {

        int nbrow = symbols.length;
        int nbcol = symbols[0].length;

       if((this.getRowCount() >0||this.getColumnCount() >0)&&
            (nbrow==getRowCount() ||nbcol==getColumnCount() ) ){ //data length match cells
                for (int i=0;i<nbrow ;i++){
                    //Cell[] arow = cells[i];
                    for (int j=0;j<nbcol ;j++){
                        Cell cell = this.getCell(i,j);
                                        cell.setSymbol(symbols[i][j]);
                    }
                }

        }
        else{//not data exists
            Cell[][] cells=new Cell[nbrow][nbcol];
            for (int i=0;i<nbrow ;i++){
                //Cell[] arow = cells[i];
                for (int j=0;j<nbcol ;j++){
                    Cell cell = new DfCell();
                    cell.setDatamodel(this);
                    cell.setSymbol(symbols[i][j]);
                    cells[i][j]  =cell;
                }
             }

            this.setDataVector(cells,new Object[nbcol]);
            updateRowColumIdentifier(nbrow,nbcol);
        }

    }
    /**
     * set number of nbrow and column
     * will override foreground color[][]
     * @param nbrow
     * @param nbcol
     */
    public void setRowColumn(int nbrow, int nbcol) {
        Cell[][] cells=new Cell[nbrow][nbcol];
        for (int i=0;i<nbrow ;i++){
            //Cell[] arow = cells[i];
            for (int j=0;j<nbcol ;j++){
                DfCell cell = new DfCell();
                cell.setForeground(this.getDefaultForeground());
                cells[i][j]  =cell;

            }
        }
        this.setDataVector(cells,new Object[nbcol]);
         updateRowColumIdentifier(nbrow,nbcol);
    }




    private void updateRowColumIdentifier(int numrow,int numcol){
        Vector rowids = this.rowIdentifiers;
        rowids.clear();
        for (int i=0;i<numrow;i++){
            rowids.add(null);
        }
        assert rowids.size() ==numrow;
        /* NOT add this, already added
        int columnCount = this.getColumnCount();
        Vector colids = this.columnIdentifiers;
        for (int i=0;i<numcol;i++){
            colids.add(null);
        }
        Vector dataVector = this.dataVector;
         columnCount = this.getColumnCount();
        System.out.println("");*/
    }


    public Cell getCell(int row, int col) {
        Cell cell = (Cell) this.getValueAt(row, col);
        cell.setDatamodel(this);
        cell.setIndex(row,col);
        return cell;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public int getNumOfCulumn() {
        return this.getColumnCount();  //To change body of implemented methods use File | Settings | File Templates.
    }

    public int getNumOfRow() {
        int rowCount = this.getRowCount();
        return rowCount ;  //To change body of implemented methods use File | Settings | File Templates.
    }


    /**************************************************
     *                convenient methods
     ***************************************************/


    /*******************************************************************************************************
     *                 add remove
     *******************************************************************************************************/

}
